package com.icloud.admin.resource;

import com.icloud.admin.application.AccessApplicationService;
import com.icloud.admin.application.command.CreateMenuCommand;
import com.icloud.admin.query.AccessQueryService;
import com.icloud.admin.application.command.ChangeMenuCommand;
import com.icloud.admin.query.dto.MenuDTO;
import com.icloud.common.InvokeResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/menus")
public class MenuController {

    @Autowired
    private AccessQueryService accessQueryService;

    @Autowired
    private AccessApplicationService accessApplicationService;
    /**
     * 查找菜单树。
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public InvokeResult findMenuTree() {
        return new InvokeResult(accessQueryService.findMenuTree());
    }

    @RequestMapping( method = RequestMethod.POST)
    public InvokeResult createMenu(@Valid @RequestBody CreateMenuCommand createMenuCommand) {
        accessApplicationService.createMenu(createMenuCommand);
        return new InvokeResult();
    }

    @RequestMapping(value = "/{menuId}", method = RequestMethod.DELETE)
    public InvokeResult removeMenu(@PathVariable("menuId") String menuId) {
        accessApplicationService.removeMenu(menuId);
        return new InvokeResult();
    }

    /**
     * 更新菜单
     * @param menuId
     * @param changeMenuCommand
     */
    @RequestMapping(value = "/{menuId}", method = RequestMethod.PUT)
    public InvokeResult changeMenuPops(@PathVariable("menuId") String menuId, @Valid @RequestBody ChangeMenuCommand changeMenuCommand) {
        changeMenuCommand.setId(menuId);
        accessApplicationService.changeMenuPops(changeMenuCommand);
        return new InvokeResult();
    }

    @RequestMapping(value = "/{menuId}", method = RequestMethod.GET)
    public InvokeResult findMenu(@PathVariable("menuId") String menuId) {
        return new InvokeResult(accessQueryService.findMenu(menuId));
    }



}
